package com.atgouwu.mall.seckill.controller;

import com.atgouwu.mall.common.utils.R;
import com.atgouwu.mall.seckill.service.SeckillService;
import com.atgouwu.mall.seckill.to.SeckillSkuRedisTo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 秒杀
 */
@Api(tags = "秒杀")
@RestController
@RequestMapping("seckill/seckill")
public class SeckillController {
    @Autowired
    private SeckillService seckillService;

    //首页获取当前时间可参与秒杀的商品redis
    @ApiOperation(value = "获取当前时间可参与秒杀的商品redis", notes = "首页获取当前时间可参与秒杀的商品进行展示")
    @GetMapping(value = "/api/getCurrentSeckillSkus")
    public R getCurrentSeckillSkus() {
        List<SeckillSkuRedisTo> vos = seckillService.getCurrentSeckillSkus();
        return R.ok().put("data", vos);
    }

    //商品详情页远程调用->根据skuId查询商品是否参加秒杀活动
    @GetMapping(value = "/sku/{skuId}")
    @ResponseBody
    public R getSkuSeckilInfo(@PathVariable("skuId") Long skuId) {
        SeckillSkuRedisTo to = seckillService.getSkuSeckilInfo(skuId);
        return R.ok().put("data", to);
    }

    //立即抢购
    @ApiOperation(value = "立即抢购", notes = "秒杀开始加入购物车变为立即抢购")
    @GetMapping(value = "/api/kill")
    public R seckill(@RequestParam("killId") String killId, @RequestParam("key") String key, @RequestParam("num") Integer num) {
        String orderSn = null;
        try {
            orderSn = seckillService.kill(killId, key, num);
            return R.ok().put("data", orderSn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }

}
